package com.dfec.water.service.impl;

import com.dfec.component.uid.core.service.UidService;
import com.dfec.framework.mybatis.core.page.PageParam;
import com.dfec.water.dao.YdbForecastDao;
import com.dfec.water.entity.YdbForecastEntity;
import com.dfec.water.entity.YdbForecastListEntity;
import com.dfec.water.service.YdbForecastListService;
import com.dfec.water.service.YdbForecastService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfec.framework.mybatis.core.page.PageUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author df
 * @title: YdbForecastServiceImpl
 * @description: 预测信息Service业务层处理
 * @date Thu Jan 23 10:20:03 CST 2025
 */
@Service("ydbForecastService")
@RequiredArgsConstructor
public class YdbForecastServiceImpl extends ServiceImpl<YdbForecastDao, YdbForecastEntity> implements YdbForecastService {

    private final YdbForecastDao ydbForecastDao;

    private final UidService uidService;

    /**
     * 分页查询
     *
     * @param pageParam 入参
     * @return PageUtils
     * @author df
     * @date Thu Jan 23 10:20:03 CST 2025
     */
    @Override
    public PageUtils queryPage(PageParam<YdbForecastEntity> pageParam) {
        return ydbForecastDao.selectPage(pageParam);
    }

    /**
     * 不带分页查询
     *
     * @param ydbForecastEntity 入参
     * @return List<YdbForecastEntity>
     * @author df
     * @date Thu Jan 23 10:20:03 CST 2025
     */
    @Override
    public List<YdbForecastEntity> listAll(YdbForecastEntity ydbForecastEntity) {
        List<YdbForecastEntity> ydbForecastEntities = ydbForecastDao.selectAll(ydbForecastEntity);
        ydbForecastEntities.forEach(item->{
            if(StringUtils.isNotBlank(item.getForecastJson())){
                //绘图数据
                item.setType("draw");
            }else {
                //边界值文件数据
                item.setType("image");
            }
        });
        return ydbForecastEntities;
    }


    @Override
    public boolean save(YdbForecastEntity entity) {
        entity.setId(uidService.next());
        return super.save(entity);
    }

    @Resource
    private YdbForecastListService forecastListService;

    //获取预测数据
    @Override
    public List<YdbForecastEntity> getForecastData() {
        List<YdbForecastEntity> ydbForecastEntities = this.ydbForecastDao.selectList();
        if(ydbForecastEntities.isEmpty()){
            return Collections.emptyList();
        }
        List<YdbForecastListEntity> list = forecastListService.list();
//        Map<Long, YdbForecastListEntity> collect = list.stream().collect(Collectors.toMap(YdbForecastListEntity::getForecastId, Function.identity()));
        ydbForecastEntities.forEach(item->{
            if(StringUtils.isNotBlank(item.getForecastJson())){
                //绘图数据
                item.setType("draw");
            }else {
                //边界值文件数据
                item.setType("image");
            }
//            item.setChildren(collect.get(item.getId()));
        });

        return ydbForecastEntities;
    }


    @Override
    public List<YdbForecastListEntity> getForecastDetailList(Long forecastId){
        YdbForecastListEntity ydbForecastListEntity = new YdbForecastListEntity();
        ydbForecastListEntity.setForecastId(forecastId);
       return forecastListService.listAll(ydbForecastListEntity);
    }
}